<html>
<head>
	<meta charset="UTF-8">
	<!--edge浏览器H5兼容设置-->
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<!--360浏览器H5兼容设置-->
	<meta name="renderer" content="webkit" />
	<title>账号管理</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--导入核心文件-->
	<script src="../bootstrap3/js/holder.js"></script>
	<link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
	<script src="../bootstrap3/jquery-1.9.1.min.js"></script>
	<script src="../bootstrap3/js/bootstrap.js"></script>
	<!-- 字体图标 -->
	<link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css" />
	<link rel="stylesheet" type="text/css" href="../css/layout.css" />
	<link rel="stylesheet" type="text/css" href="../css/top.css" />
	<link rel="stylesheet" type="text/css" href="../css/footer.css" />
	<link rel="shortcut icon" href="../favicon.ico"/>
	<script src="../js/adddel.js" type="text/javascript" charset="utf-8"></script>
	<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
</head>
<body>
<div class="container">
	<div class="col-md-10">
		<div class="panel panel-default">
			<div class="panel-body">
				<table class="table table-striped">
					<caption>账号管理</caption><a href="login.html" onclick="logout()">注销登录</a>
					<thead>
					<tr>
						<th>ID</th>
						<th>用户名</th>
						<th>注册时间</th>
						<th>状态</th>
						<th colspan="3">操作</th>
					</tr>
					</thead>
					<tbody id="address-list">
					<tr>
						<td>x</td>
						<td>x</td>
						<td>xxx</td>
						<td><a class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> xx</a></td>
						<td><a class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> xx</a></td>
						<td><a class="btn btn-xs add-def btn-default">设为默认</a></td>
					</tr>
					</tbody>
				</table>
				<hr>
				<table class="layui-table" id='table' lay-filter="table1">
				</table>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	//加载页面的时候自动加载这个方法
	$(document).ready(function () {
		showAddressList();
		ws();
		//轮询：保持刷新，每50一次
		setInterval("showAddressList()", 1000*50);
	});

	function ws(){
		//判断浏览器是否支持WebSocket
		const supportsWebSockets = 'WebSocket' in window || 'MozWebSocket' in window;
		if (supportsWebSockets) {
			//建立WebSocket连接（ip地址换成自己主机ip）
			const ws = new WebSocket("ws://"+returnCitySN["cip"]+"/ws");
			ws.onopen = function(){
				//当WebSocket创建成功时，触发onopen事件
				console.log("websocket连接成功");
				//ws.send("hello"); //将消息发送到服务端
				ws.send(
						/*JSON.stringify({
							symbols: "['00700.hk]",
							command:0,
							version:'1.0.0',
							token:'szfiu',
							heartBeat:'heartBeat'
						})*/

						JSON.stringify({
							MyName:'adminIndex'
						})
				);
			}
			ws.onmessage = function(e){
				//当客户端收到服务端发来的消息时，触发onmessage事件，参数e.data包含server传递过来的数据
				console.log(e.data);
				alert(e.data);
			}
			ws.onclose = function(e){
				//当客户端收到服务端发送的关闭连接请求时，触发onclose事件
				console.log("websocket已断开");
			}
			ws.onerror = function(e){
				//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
				console.log("websocket发生错误"+e);
			}
		}else{
			// layer.alert("您的浏览器不支持 WebSocket!");
			alert("您的浏览器不支持WebSocket!");
		}
	}

	//查看当前用户的地址列表具体方法
	function showAddressList() {
		$("#address-list").empty();
		$.ajax({
			url: "/users/",
			type: "GET",
			dataType: "JSON",
			success: function (json) {
				let list = json.data;
				let state;

				for (let i = 0; i < list.length; i++) {
					let address = `
					<tr>
					<td>#{uid}</td>
					<td>#{name}</td>
					<td>#{time}</td>
					<td>#{state}</td>
					<td><a onclick="deleteFileByUid('${list[i].uid}')" class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> 删除用户文件</a></td>
					<td><a onclick="deleteUserByUid('${list[i].uid}')" class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> 删除用户账号</a></td>
					<td><a onclick="offlineUser('${list[i].uid}')" class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> 强制用户下线</a></td>
					</tr>
					`

					switch (list[i].state){
						case 0:
							state = "<div style='color: #b92c28'>离线";
							break;
						case 1:
							state = "<div style='color: #00B83F'>在线";
							break;
						case 2:
							state = "<div style='color: #9EA8A2'>被删除";
							break;
						default:
							state = "<div style='color: #c0a16b'>其他";
							break;
					}

					//替换
					address = address.replace("#{uid}", list[i].uid);
					address = address.replace("#{name}", list[i].username);
					address = address.replace("#{time}", list[i].createTime);
					address = address.replace("#{state}", state);
					//放入
					$("#address-list").append(address);
				}
				//第一条数据隐藏 设为默认 按钮
				$(".add-def:eq(0)").hide();
			},
			error:function (xhr){
				alert("查看用户信息失败！")
			}
		});
	}

	function deleteFileByUid(uid) {
		alert("确定要删除该用户的文件吗？")
		$.ajax({
			url: "/fileList/delFileByUid/"+uid ,
			type: "GET",
			dataType: "JSON",
			success: function(json) {
				if (json.state == 200) {
					showAddressList();
				} else {
					alert("删除失败！" + json.message);
				}
			},
			error: function(json) {
				alert("删除请求返回异常！" + json.status);
			}
		});
	}
	function deleteUserByUid(uid) {
		$.ajax({
			url: "/users/deleteUserByUid/"+uid ,
			type: "POST",
			dataType: "JSON",
			success: function(json) {
				if (json.state == 200) {
					alert("确定要删除该用户吗？")
					showAddressList();
				} else {
					alert("删除失败！" + json.message);
				}
			},
			error: function(json) {
				alert("删除请求返回异常！" + json.status);
			}
		});
	}
	function offlineUser(uid) {
		$.ajax({
			url: "/login/offline/"+uid ,
			type: "GET",
			dataType: "JSON",
			success: function(json) {
				if (json.state == 200) {
					alert("确定要强制该用户下线吗？")
					showAddressList();
				} else {
					alert("强制下线失败！" + json.message);
				}
			},
			error: function(json) {
				alert("强制下线请求返回异常！" + json.status);
			}
		});
	}

	function logout(){
		$.ajax({
			url: "/login/logout",
			type: "POST",
			data: new FormData($("#form-change-avatar")[0]),
			dataType: "JSON",
			processData: false, // 关闭processData处理数据
			contentType: false, // 关闭contentType发送数据的格式
			success: function(json) {
				if (json.state == 200) {
					location.href = "login.html";
				} else {
					alert("登出失败！" + json.message);
				}
			},
			error: function(json) {
				alert("登出请求异常！状态码：" + json.status);
			}
		});
	}



</script>
</body>
</html>